package com.m3839.sdk.ym.core

import android.os.Build
import android.os.Handler
import android.os.Looper
import android.support.annotation.RequiresApi
import com.m3839.sdk.ym.util.YMLogUtils

/**
 * @description: YMHeartController
 *
 * @author ChenBaoyang
 * @date 6/1/21 11:32
 */
class YMHeartController : IYMHeartController() {

    companion object{
        /**
         * 持续心跳
         */
        const val MSG_HEART = 10001
    }

    /**
     * 是否暂停
     */
    private var isPause = false

    /**
     * 是否当前在夜间模式
     */
    private var inNightMode = false

    /**
     * 无视夜间模式
     */
    private var ignoreNightMode = false

    /**
     * 判断是否运行了
     */
    private var isRunning = false

    /**
     * 监听
     */
    private var listener: OnHeartListener? = null

    /**
     * 当天的日期
     */
    private var todayDate: String? = null

    override fun init(
        interval: Int,
        listener: OnHeartListener
    ) {
        this.interval = interval
        this.listener = listener
    }

    override fun start() {
        mHandle.sendEmptyMessage(MSG_HEART)
    }

    override fun pause(isPause: Boolean) {
        this.isPause = isPause
        //从夜间时间调成日间时间,Activity还是夜间模式处理
        val canPlay: Boolean = YMDateManager.instance.betweenPlayTime()
        if (inNightMode && canPlay) {
            inNightMode = false
            listener!!.onNightMode(inNightMode)
        }
        if(!isPause) {
            mHandle.sendEmptyMessage(MSG_HEART)
        }else{
            mHandle.removeMessages(MSG_HEART)
        }
    }

    override fun stop() {
        currentSecond = 0
        mHandle.removeMessages(MSG_HEART)
        isRunning = false
        isPause = false
    }

    override fun setIgnoreNightMode(ignoreNightMode: Boolean) {
        this.ignoreNightMode = ignoreNightMode
        if (ignoreNightMode) {
            inNightMode = false
            mHandle.sendEmptyMessage(MSG_HEART)
        }
    }

    /**
     * 用来处理心跳
     */
    @RequiresApi(Build.VERSION_CODES.CUPCAKE)
    private val mHandle = Handler(Looper.myLooper()!!) {
        if(it.what == MSG_HEART) {
            onHeartEvent()
        }
        true
    }

    /**
     * 处理心跳的事件
     */
    @RequiresApi(Build.VERSION_CODES.CUPCAKE)
    private fun onHeartEvent() {
        if (interval <= 0) {
            //如果心跳间隔小于0
            stop()
            return
        }

        //检查一下是否是在夜间模式下
        val canPlay: Boolean = YMDateManager.instance.betweenPlayTime()
        if (!canPlay && !ignoreNightMode) {
            inNightMode = true
            listener!!.onNightMode(inNightMode)
            return
        }

        //检查一下否过了凌晨到第二天
        val currentDate: String = YMDateManager.instance.todayDate
        if (currentDate != todayDate) {
            YMLogUtils.flog("onNewDay = $todayDate")
            todayDate = currentDate
            listener!!.onNewDay()
        }

        //做剩余游戏时长的递减
        remainingTime--
        if (remainingTime <= 0) {
            currentSecond = 0
            //倒计时结束，心跳回调, 请求服务端
            listener!!.onBeat(remainingTime)
            stop()
            return
        }

        YMLogUtils.flog("currentSecond = $currentSecond")
        currentSecond++
        if (currentSecond >= interval && currentSecond % interval == 0 && remainingTime > 0) {
            //心跳间隔回调
            listener!!.onBeat(remainingTime)
        }

        for (i in moments.indices) {
            val moment = moments[i]
            if (moment.toLong() == remainingTime && listener != null) {
                YMLogUtils.flog("m3839", "倒计时 -->$remainingTime")
                //检查是否到倒计时弹窗或者气泡的点
                listener!!.onCountDown(moment)
                break
            }
        }

        mHandle.sendEmptyMessageDelayed(MSG_HEART, 1000)
    }
}